import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int T = scanner.nextInt();
        scanner.nextLine();
        for (int i = 0; i < T; i++) {
            String str = scanner.nextLine();
            int count = outTrue(str);
            System.out.println(count);
        }
    }

    private static int outTrue(String str) {
       int count = 0;
       int state = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c > 'Z'){
                if (state == 0){
                    count ++;
                }else {
                    count += 2;
                    state = 0;
                }
            }else {
                if (state == 0){
                    count += 2;
                    state = 1;
                }else {
                    count ++;
                }
            }
        }
        return count;
    }
}
